package com.qmhl.toolposition.api

import android.app.Activity
import android.content.Intent
import android.os.Handler
import android.os.Looper
import com.google.gson.Gson
import com.qmhl.toolposition.MyApplication
import com.qmhl.toolposition.activity.VipActivity
import com.qmhl.toolposition.api.content.AppCommonResponse
import okhttp3.Interceptor
import okhttp3.Response
import java.nio.charset.Charset


class BodyInterceptor : Interceptor {

    companion object {
        const val TAG = "BodyInterceptor"
    }

    private val gson = Gson()
    private val handler = Handler(Looper.getMainLooper())

    override fun intercept(chain: Interceptor.Chain): Response {
        val charset = Charset.forName("UTF-8")
        // 打印请求报文
        val request = chain.request()
        val response = chain.proceed(request)
        val responseBody = response.body()
        val source = responseBody?.source()
        source?.request(Long.MAX_VALUE)
        val buffer = source?.buffer()
        val respBody = buffer?.clone()?.readString(charset)

    /*    try {
            val parseObject =
                com.alibaba.fastjson.JSONObject.parseObject(respBody, AppCommonResponse::class.java)
            var code = parseObject.code
            if (code == 10086) {
                val intent = Intent(MyApplication.instant, VipActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                MyApplication.instant.startActivity(intent)
                var activity=MyApplication.instant as Activity
                activity.finish()
            }
        } catch (e: Exception) {
            println(111)
        }*/


        parseAuthError(respBody)
        return response
    }

    private fun parseAuthError(respBody: String?) {
        // 处理自身的业务逻辑
    }
}
